package cn.itcast.demo.dao;

import cn.itcast.demo.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class UserFackDao {
    @Autowired
    private RestTemplate restTemplate;



    @HystrixCommand(fallbackMethod = "queryUserByIdFallback")//当queryUser执行失败时要回调，回调方法，返回一个降级后的消息
    public User queryUserById(Long id){

        // 地址直接写服务名称即可
        String baseUrl = "http://user-server/user/";

        //发起web请求查询，并把返回结果转成User对象并添加到集合中
       return restTemplate.getForObject(baseUrl+id,User.class);
    }

    public User queryUserByIdFallback(Long id){

        User user = new User();

        user.setId(id);
        user.setName("抱歉，当前查询失败，请稍后重试");

        return user;
    }
}
